package bean;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import biz.coursebiz;
import entity.Courses;
import entityBean.CoursesFacade;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author ADMIN
 */
@ManagedBean
@RequestScoped
public class CoursesBean {

    @EJB
    private CoursesFacade coursesFacade;
    private Courses courses = new Courses();
    private List<Courses> list = new ArrayList<Courses>();
    coursebiz coursebiz = new coursebiz();

    public CoursesBean() {
        list = coursebiz.listCourses();
    }

    public String insert() {
        coursesFacade.create(courses);
        list = coursebiz.listCourses();
        return "listcourses";
    }

    public String delete(Courses courses) {
        coursesFacade.remove(courses);
        list = coursebiz.listCourses();
        return "listcourses";
    }

    public String edit() {
        try {
            Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
            int id = Integer.parseInt(params.get("id"));
            coursesFacade.update(courses.getCname(), id);
            list = coursebiz.listCourses();
        } catch (Exception e) {
        }

        return "listcourses";
    }

    public Courses getCourses() {
        return courses;
    }

    public void setCourses(Courses courses) {
        this.courses = courses;
    }

    public List<Courses> getList() {
        return list;
    }

    public void setList(List<Courses> list) {
        this.list = list;
    }
}
